library(tidyverse)

# quantms ---------
sdrf <- read_tsv('~/append-ssd/nextflowing/xiangya.b1.sdrf.tsv')

# multiply fraction 1 to 30 fractions
b1all <- 2:30 |>
  as.character() |>
  map(\(x)sdrf |>
        mutate(`comment[data file]` = str_replace(`comment[data file]`, '1(?=.raw)', x),
               `assay name` = str_replace(`assay name`, '1', x),
               `comment[fraction identifier]` = x |> as.numeric())) |>
  list_rbind() |>
  bind_rows(sdrf)

b1all |>
  mutate(`comment[file uri]` = `comment[data file]`,
         `characteristics[cell type]` = `source name`) |>
  write_tsv('~/append-ssd/nextflowing/xiangya.b1all.sdrf.tsv')

## merge 3 into 1 -----------
sdrf_all3 <- list.files('~/append-ssd/nextflowing/', 'all.sdrf', full.names = T) |>
  map(read_tsv)

sdrf_all3 |>
  bind_rows() |>
  select(-contains('...')) |>
  mutate(`comment[cleavage agent details]` = 'AC=MS:1001313;NT=Trypsin',batch = 0:1349 %/% 450,
         `comment[fraction identifier]` = `comment[fraction identifier]` + batch*15,
         `assay name` = str_c('run', `comment[fraction identifier]`),
         `comment[data file]` = str_c('mzml', batch+1, '/', `comment[data file]`),
         `comment[file uri]` = `comment[data file]`,
         `characteristics[cell type]` = `source name`) |>
  select(-batch) |>
  write_tsv('~/append-ssd/nextflowing/xiangya.b123all.sdrf.tsv')
  

# openms -----------
read_delim('~/append-ssd/nextflowing/quantms_xy_b1all_canon_msgf/sdrfparsing/xiangya.b1all.sdrf_openms_design.tsv')

opemss <- list.files(path = '~/append-ssd/nextflowing/',recursive = T,
           pattern = 'openms_design.tsv', full.names = T) |>
  str_subset('all_canon_msgf')

opemss[[1]] |>
  read_tsv()
